/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import entity.Regalo;
import entity.Utente;
import facade.RegaloFacadeLocal;
import facade.UtenteFacadeLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author erola
 */
public class GestioneLista extends HttpServlet {

    @EJB
    private UtenteFacadeLocal utenteFacade;
    @EJB
    private RegaloFacadeLocal regaloFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");

        HttpSession session = request.getSession();
        Long uid = Long.valueOf(session.getAttribute("user_id").toString());
        Utente u = utenteFacade.find(uid);

        //CANCELLAZIONE DI UN DESIDERIO DALLA LISTA
        if (action != null && action.equals("canc")) {
            try {
                cancella(u, request.getParameter("item"));
                session.setAttribute("regali", u.getLista());
            } catch (Exception e) {
            } finally {
                RequestDispatcher rq = request.getRequestDispatcher("/account.jsp");
                rq.forward(request, response);
            }
        } ////////////
        else {
            try {
                session.setAttribute("regali", u.getLista());
            } catch (Exception e) {
            } finally {
                RequestDispatcher rq = request.getRequestDispatcher("/account.jsp");
                rq.forward(request, response);
            }
        }
    }

    protected void cancella(Utente u, String id) throws NullPointerException {
        ArrayList<Regalo> regali = u.getLista();
        int i = 0;
        while (i < regali.size() && !(regali.get(i).getId().toString()).equals(id)) {
            i++;
        }
        Regalo r = regali.remove(i);
        u.setLista(regali);
        regaloFacade.remove(r);
        utenteFacade.edit(u);
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);




    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);




    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";


    }// </editor-fold>
}
